package com.jieshen.chat;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class Receive implements Runnable{
    //用于接收数据的数据流
    private DataInputStream dis;
    private boolean flag = true;
    public Receive(Socket client){
        try {
            //我认为，这个DataInputStream，可以拿到所有的数据
            dis = new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            flag = false;
            CloseUtil.closeAll(dis,client);
        }
    }

    private String getMessage(){
        String str = "";
        try {
            str = dis.readUTF();
        } catch (IOException e) {
            flag = false;
            CloseUtil.closeAll(dis);
        }
        return str;
    }
    @Override
    public void run() {
        while (flag){
            System.out.println(this.getMessage());
        }
    }
}
